package com.shengyuan.manage_os.old.ying.boce.jsy;

import java.io.File;

import com.shengyuan.manage_os.im.ECApplication;
import com.shengyuan.manage_os.old.ying.boce.task.GenericTask;
import com.shengyuan.manage_os.old.ying.boce.task.TaskAdapter;
import com.shengyuan.manage_os.old.ying.boce.task.TaskListener;
import com.shengyuan.manage_os.old.ying.boce.task.TaskParams;
import com.shengyuan.manage_os.old.ying.boce.task.TaskResult;
import com.shengyuan.manage_os.old.ying.erpm.Actual_System_Setting;
import com.shengyuan.manage_os.old.ying.erpm.MainApplication;
import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_Data_Table;
import com.shengyuan.manage_os.old.ying.erpm.db.CLS_Actual_Data_Table;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;

@SuppressLint("HandlerLeak")
public class AdminPicForceHandlerTask {
	public static  AdminPicForceHandlerTask pMobileHandlerTask;
	public int  m_id, fujian_id , timeout = 60000;
	private CLS_Actual_Data_Table cls_Data;
	private boolean stop ;
	
	private ReNameTask mReNameTask; 
	private Handler uiHandler1 = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			mReNameTask = new ReNameTask();
			mReNameTask.setListener(mReNameTaskListener);
			mReNameTask.execute();
		}
	};
	
	public void start() {
		stop = false;
		uiHandler1.sendEmptyMessageDelayed(1, 2 * 1000);
	}
	
	public void stop(){
		stop = true;
	}


	private TaskListener mReNameTaskListener = new TaskAdapter() {
		private ProgressDialog	mDialog;
		@Override
		public void onPostExecute(GenericTask task, TaskResult result) {
			if (mDialog!=null) {
				mDialog.dismiss();
			}
			
//			if (result == TaskResult.OK) {
////				Utility.MessageBox("已经完成目录清空");
//			} else if (result == TaskResult.CANCELLED) {
////				Utility.MessageBox(MainApplication.Instance.getString(R.string.admin_status_invalid_username_or_password));
//			}
		}
		
		@Override
		public void onProgressUpdate(GenericTask task, Object param) {
			if (mDialog!=null) {
				mDialog.setMessage("目录ID:" + param.toString());
			}
		}

		@Override
		public void onPreExecute(GenericTask task) {
			if (Actual_System_Setting.Instance!=null) {
				mDialog = Utility.ShowModelDialog(Actual_System_Setting.Instance, 
						MainApplication.Instance.getString(R.string.admin_status_logging_in));
				mDialog.show();	
			}
		}
	};
	private class ReNameTask extends GenericTask
	{
		@Override
		protected TaskResult _doInBackground(TaskParams... params) {
			SendPicFuJianToUpLoadNet();
			return TaskResult.OK;
		}
	
		private void SendPicFuJianToUpLoadNet() {
			fujian_id =1;
			Actual_Data_Table dbo = new Actual_Data_Table(ECApplication.Instance);
			try {
					Cursor cursor = dbo.get_Sended_all_Cursor("2");
				System.out.println("############==="+cursor.getCount());
				 while (cursor.moveToNext()) {
					fujian_id = cursor.getInt(0);
					if (fujian_id >0) {
						publishProgress(fujian_id);
							//建立 uploadfile/yearmoth/idtime/json1
						String subpath = MainApplication.filePath ;
						File subFile = new File(subpath); 
						if (subFile.exists() && subFile.isDirectory()) {
							String sb = subpath + fujian_id;
							String sb1 = subpath + fujian_id + "/json1";
							String sb2 = subpath + fujian_id + "/json2";
							
							File dirsb1 = new File(sb1);
							if (dirsb1.exists()) {
								File[] sfile = dirsb1.listFiles();
								if (sfile.length==0) {
									dirsb1.delete();
								}
							}
							File dirsb2 = new File(sb2);
							if (dirsb2.exists()) {
								File[] sfile = dirsb2.listFiles();
								if (sfile.length==0) {
									dirsb2.delete();
								}
							}
							dirsb2 = new File(sb);
							if (dirsb2.exists()) {
								File[] ss = dirsb2.listFiles();
								if (ss.length==0) {
									dirsb2.delete();
									dbo.Update_Data(fujian_id, dbo.SERVICEID, "2");
								}else {
									dbo.Update_Data(fujian_id, dbo.SERVICEID, "1");
								}
							}else {
								//dbo.Update_Data(fujian_id, dbo.SERVICEID, "2");
							}
							
						}
					}else {
						return;
					}
				}			
			} catch (Exception e) {
				Utility.MessageBox(e.getMessage());
			}
			dbo.close();
		}
	}	

}
